package com.lmscn.lms.api.model;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by quxingyu on 2015/8/6.
 */
public class QuestionFavorite extends Model<QuestionFavorite> {
	public static final QuestionFavorite dao = new QuestionFavorite();

	/**
	 * 查询题目是否已经收藏过
	 * 
	 * @date 2016年12月22日15:21:15
	 * @param studentId
	 * @param questionId
	 * @return
	 */
	public QuestionFavorite getFavorite(long studentId, Long questionId) {
		String sql = "select * from t_question_favorite t where t.student_id = ? and t.question_id = ? ";
		return dao.findFirst(sql, studentId, questionId);
	}

	/**
	 * 保存收藏试题
	 * 
	 * @param questionFavorite
	 * @param question
	 * @param questionCategory
	 * @param testpaperResult
	 */
	public void saveQuestionFavorite(QuestionFavorite questionFavorite, Question question, QuestionCategory questionCategory,
			TestpaperResult testpaperResult, String type) {
		questionFavorite.set("question_id", Long.valueOf(question.get("id").toString()));
		questionFavorite.set("target_type", type);
		questionFavorite.set("target_id", Long.valueOf(testpaperResult.get("id").toString()));
		questionFavorite.set("student_id", Long.valueOf(testpaperResult.get("student_id").toString()));
		questionFavorite.set("created_time", new Date());
		questionFavorite.set("question_type", question.get("type"));
		questionFavorite.set("question_title", question.get("title"));
		questionFavorite.set("question_category_id", Long.valueOf(questionCategory.get("id").toString()));
		questionFavorite.set("question_category_name", questionCategory.get("name"));
		questionFavorite.remove("id").save();
	}
	public void saveCourseTestQuestionFavorite(QuestionFavorite questionFavorite, Question question, QuestionCategory questionCategory,
			Long studentId,Long courseId, String type) {
		questionFavorite.set("question_id", Long.valueOf(question.get("id").toString()));
		questionFavorite.set("target_type", type);
		questionFavorite.set("target_id", courseId);
		questionFavorite.set("student_id", studentId);
		questionFavorite.set("created_time", new Date());
		questionFavorite.set("question_type", question.get("type"));
		questionFavorite.set("question_title", question.get("title"));
		questionFavorite.set("question_category_id", Long.valueOf(questionCategory.get("id").toString()));
		questionFavorite.set("question_category_name", questionCategory.get("name"));
		questionFavorite.remove("id").save();
	}

	/**
	 * 根据ID集合删除收藏
	 * 
	 * @param id
	 */
	public void deleteQuestionFavorite(Long id) {
		Db.update("delete from t_question_favorite where id = ?", id);
	}

	public int deleteMyCollection(Long questionId, Long studentId) {
		return Db.update("delete from t_question_favorite where question_id =? and student_id = ? ", questionId, studentId);
	}

	/**
	 * 获取题目收藏列表
	 * 
	 * @param pageNumber
	 * @param pageSize
	 * @param type
	 * @param key
	 * @param targetType
	 * @return
	 */
	public Page<QuestionFavorite> getQuestionFavorites(int pageNumber, int pageSize, String type, String key, String targetType) {
		StringBuffer sb = new StringBuffer();
		List<Object> para = new ArrayList();
		sb.append(" from t_question_favorite where 1=1");
		if (!"".equals(type) && !"null".equals(type) && type != null) {
			sb.append(" and t.question_type = ?");
			para.add(type);
		}
		if (!"".equals(targetType) && !"null".equals(targetType) && targetType != null) {
			sb.append(" and t.target_type = ?");
			para.add(targetType);
		}
		if (!"".equals(key) && !"null".equals(key) && key != null) {
			sb.append(" and t.question_title like ?");
			para.add("%" + key + "%");
		}
		sb.append(" order by t.created_time desc");
		return dao.paginate(pageNumber, pageSize, "select t.* ", sb.toString(), para.toArray());

	}
}
